<?php
/**
 * Base_App
 *
 * LICENSE
 *
 * This source file is subject to the new CC-GNU LGPL
 * It is available through the world-wide-web at this URL:
 * http://creativecommons.org/licenses/LGPL/2.1/
 *
 * @category   Base_App
 * @copyright  Copyright (c) 2009 Alexander fanatique* Thomas (me@alexander-thomas.net)
 * @license    http://creativecommons.org/licenses/LGPL/2.1/
 */
class Base_App_Controller_Action extends Zend_Controller_Action {
	protected $_log;
	protected $_session;
	protected $_appConfig;
	protected $_auth;
	
	/**
	 * Class constructor
	 *
	 * The request and response objects should be registered with the
	 * controller, as should be any additional optional arguments; these will be
	 * available via {@link getRequest()}, {@link getResponse()}, and
	 * {@link getInvokeArgs()}, respectively.
	 *
	 * When overriding the constructor, please consider this usage as a best
	 * practice and ensure that each is registered appropriately; the easiest
	 * way to do so is to simply call parent::__construct($request, $response,
	 * $invokeArgs).
	 *
	 * After the request, response, and invokeArgs are set, the
	 * {@link $_helper helper broker} is initialized.
	 *
	 * Finally, {@link init()} is called as the final action of
	 * instantiation, and may be safely overridden to perform initialization
	 * tasks; as a general rule, override {@link init()} instead of the
	 * constructor to customize an action controller's instantiation.
	 *
	 * @param Zend_Controller_Request_Abstract $request
	 * @param Zend_Controller_Response_Abstract $response
	 * @param array $invokeArgs Any additional invocation arguments
	 * @return void
	 */
	public function __construct(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response, array $invokeArgs = array ()) {
		parent :: __construct($request, $response, $invokeArgs);
		$appPath = Zend_Registry::get('applicationPath');
		$this->_appConfig = Zend_Registry::get('applicationConfiguration');
		$this->view->baseUrl = $this->_appConfig->base->url;
		$this->view->pageTitle = $this->_appConfig->base->title;
		$this->_log = Zend_Registry::get('log');
		$this->_session = new Zend_Session_Namespace('user');
		$this->_auth = Zend_Auth::getInstance();
		$module = $this->_getParam('module');
		$this->view->setScriptPath(array($appPath.'Views/Scripts/', $appPath.'Views/Scripts/'.strtolower($module).'/'));
		$this->view->setEncoding('utf-8');
		$this->view->addHelperPath($appPath.'Views/Helper/', 'My_View_Helper');
	}
	/**
	 * On pre dispatch the privileges of the current user are checked
	 * against the ACL.
	 */
	public function preDispatch() {
		//Check ACL
		if($this->_appConfig->acl->state != 0){
			$controller = $this->_getParam('controller');
			$action = $this->_getParam('action');
			$acl = Zend_Registry::get('acl');
			if (empty ($this->_session->user['role'])) {
				$role = 'guest';
			}else {
				$role = $this->_session->user['role'];
			}

			if ($this->_session->user['role'] != 'administrator' && !$acl->isAllowed($role, $controller, $action)) {
				$this->_log->err("$role has no right to access $controller $action");
				throw new Base_App_Acl_Exception(Base_App_Acl_Exception::INSUFFICIENT_PRIVILEGES);
			}			
		}
		if ($this->getRequest()->isXmlHttpRequest()) {
			$this->_helper->layout->setLayout('blank');
		}
		$this->view->messages = $this->_helper->FlashMessenger->getMessages();
	}
}
?>
